<script type="text/javascript">
$.extend({
    refresh_task_list: function() {
        if ($('#tabs_load').hasClass("event_load")) {
            $('#grid_alert_table').trigger("reloadGrid");
        }
    }
});

if (window.interval_task_list) {} else {
    window.interval_task_list = window.setInterval("$.refresh_task_list()", 1000000000000);
}

$(function() {
    $('#grid_alert_table').jqGrid({
        url: '/alert/grid',
        datatype: 'json',
        mtype: 'GET',
        styleUI: 'Bootstrap',
        colNames: ['编号', '返回码', "[['label alert'|translate]]", "[['label event'|translate]]", "[['label location'|translate]]", "[['label location'|translate]]", "[['label state'|translate]]", 'is_handled', "[['label time'|translate]]", ],
        colModel: [{
            name: 'id',
            index: 'id',
            width: 5,
            hidden: true
        }, {
            name: 'returncode',
            index: 'returncode',
            width: 5,
            hidden: true
        }, {
            name: 'detail',
            index: 'detail',
            width: 25,
            formatter: function(cellvalue, option, row) {
                return makeEllipsis(cellvalue,option);
            },
            unformat: function(cellvalue) {
                return cellvalue;
            }
        }, {
            name: 'level',
            index: 'level',
            width: 6,
            formatter: function(cell, option, row) {
                var html = '';
                switch (cell) {
                    case 'WARNING':
                    case 'WARN':
                        html += '<button type="button" class="btn btn-xs btn-warning">WARNING</button>';
                        break;
                    case 'ERROR':
                        html += '<button type="button" class="btn btn-xs btn-danger">' + cell + '</button>';
                        break;
                    case 'INFO':
                        html += '<button type="button" class="btn btn-xs btn-success">' + cell + '</button>';
                }
                return html;
            },
            unformat: function(cell) {
                return cell;
            }
        }, {
            name: 'ip',
            index: 'ip',
            width: 6
        }, {
            name: 'detail',
            index: 'detail',
            width: 10,
            hidden: true
        }, {
            name: 'is_handled',
            index: 'is_handled',
            width: 6,
            formatter: function(cell, option, row) {
                row = row2object(row, option.gid);
                var is_handled_show = '';
                var is_handled_sql = row['is_handled_sql'];
                if (is_handled_sql == 'False') {
                    is_handled_show += '<button type="button" class="btn btn-xs btn-danger">未确认</button>'
                } else {
                    is_handled_show += '<button type="button" class="btn btn-xs btn-success">已确认</button>'
                }
                return is_handled_show;
            },
            unformat: function(cell) {
                return cell;
            }
        }, {
            name: 'is_handled_sql',
            index: 'is_handled_sql',
            width: 5,
            hidden: true
        }, {
            name: 'created_at',
            index: 'created_at',
            width: 8
        }],
        page: 1,
        rowNum: 10,
        sortname: 'id',
        sortorder: 'desc',
        altclass: 'ui-priority-secondary',
        altRows: true,
        autowidth: true,
        multiselect: true,
        multiboxonly:true,
        multiselectWidth:'60px',
        //height:mainpanelHeight,
        beforeSelectRow:function(rowid, e) {
            if($(e.target).is('input')) {
                return true;
            }
            return false;
        },
        gridComplete: function() {
            var gridData = $(this).jqGrid('getRowData');
            jqgrid_page($(this), gridData);
            jqgrid_resize($(this));
        },
        beforeRequest: function(argument) {
        }
    });
});

var dbg_messsge_obj = {
    1002: '进程异常退出',
    1004: '进程assert',
    1005: '连接 admin 失败',
    1006: "重启时间过长警告",
    1007: "集群心跳超时警告",
    1008: "网络fence失败",
    3000: "磁盘离线",
    3004: "磁盘读写错误",
    3005: "元数据读写错误",
    //    3006: "磁盘损坏扇区个数",
    //    3007: "暂时不支持的raid卡型号",
    4011: "设置虚拟 IP 错误",
    5005: "数据修复警告",
    5007: "数据加载警告",
    //    5006: "volume",
    5008: "chunk数据加载警告",
    5006: '磁盘空间已满',
    6002: "授权信息错误",
    9100: "iSCSI服务错误",
    9200: "NBD服务错误",
}

function message_to_show(grid_table_obj, id) {
    var returncode = parseInt(grid_table_obj.getCell(id, "returncode"));
    var detail = dbg_messsge_obj[returncode];
    if (typeof(detail) != 'undefined') {
        grid_table_obj.jqGrid('setRowData', id, {
            detail: detail
        });
    }
}

$('#handle_alert_button').click(function() {
    var rowIds = $("#grid_alert_table").jqGrid('getGridParam', 'selarrrow');
    if (rowIds == '') {
        SelfAlert('请先选择一条告警记录');
        return false;
    }
    var ids = ''
    for (var id = 0; id < rowIds.length; id++) {
        ids += rowIds[id] + ','
    }
    $.post("/alert/handler", {
        'ids': ids
    }, function(data) {
        if (data.reply.is_success) {
            x = 0
        } else {
            error = data.reply.error;
            SelfAlert(error);
        }
        $("#grid_alert_table").trigger('reloadGrid');
        reload_warning_num();
    });
});
</script>
<div class="panel panel-f-style">
    <div class="panel-body">
        <div class="title-tag">
            <h3>告警</h3>
            <p>告警管理</p>
        </div>
        <div class="grid-tools">
            <div class="grid-operate pull-left">
                <button class="btn btn-blue" type="button" id="handle_alert_button">标记为已确认</button>
            </div>
        </div>
        <div class="grid-body" id="gridttt">
            <table id="grid_alert_table"></table>
        </div>
    </div>
</div>
